<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page language="java" import="java.sql.*"%>
<%
	Class.forName("org.postgresql.Driver");
	Connection conn = DriverManager.getConnection(
			"jdbc:postgresql://localhost:5432/cse132b", "cse132blogin",
			"1234567890");
	Statement stmt, stmt1, stmt2;

	if (request.getParameter("ACTION") != null) {
		if (request.getParameter("ACTION").equals("Insert")) {
			int NewID = 0;

			PreparedStatement statement = conn
					.prepareStatement("INSERT INTO review_sections(section_id, date, time, building_room, mandatory) VALUES (?,?,?,?,?)");
			statement.setInt(1, Integer.parseInt(request
					.getParameter("sltClasses")));
			statement.setString(2, request.getParameter("txtDate"));
			statement.setString(3, request.getParameter("txtTime"));
			statement.setString(4, request.getParameter("txtRoom"));

			if (request.getParameter("chkMandatory") == null)
				statement.setBoolean(5, false);
			else
				statement.setBoolean(5, true);

			statement.executeUpdate();
			statement.close();

		}

		if (request.getParameter("ACTION").equals("Update")) {
			int EditID = Integer.valueOf(request
					.getParameter("PARAMETER"));

			PreparedStatement statement = conn
					.prepareStatement("UPDATE review_sections SET date = ?, time = ?, building_room = ?, mandatory = ? WHERE id = ?");

			statement.setString(
					1,
					request.getParameter("txtDate"
							+ String.valueOf(EditID)));
			statement.setString(
					2,
					request.getParameter("txtTime"
							+ String.valueOf(EditID)));

			statement.setString(
					3,
					request.getParameter("txtRoom"
							+ String.valueOf(EditID)));

			if (request.getParameter("chkMandatory"
					+ String.valueOf(EditID)) == null)
				statement.setBoolean(4, false);
			else
				statement.setBoolean(4, true);

			statement.setInt(5, EditID);
			statement.executeUpdate();
			statement.close();
		}

		if (request.getParameter("ACTION").equals("Delete")) {
			int EditID = Integer.valueOf(request
					.getParameter("PARAMETER"));
			
			PreparedStatement statement = conn
					.prepareStatement("DELETE FROM review_sections WHERE id = ?");
			statement.setInt(1, EditID);
			statement.executeUpdate();
			statement.close();
		}
	}
%>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
	function Insert() {
		document.getElementById("sltClasses").required = true;
		document.getElementById("txtDate").required = true;
		document.getElementById("txtTime").required = true;
		document.getElementById("txtRoom").required = true;
		document.getElementById("ACTION").value = "Insert";
	}
	function Update(row) {
		document.getElementById("txtDate"+row).required = true;
		document.getElementById("txtTime"+row).required = true;
		document.getElementById("txtRoom"+row).required = true;
		document.getElementById("ACTION").value = "Update";
		document.getElementById("PARAMETER").value = row;
	}
	function Delete(row) {
		document.getElementById("ACTION").value = "Delete";
		document.getElementById("PARAMETER").value = row;
	}
</script>
</head>
<body>
	<table border="1">
		<tr>
			<td valign="top" width="40%">
				<%-- -------- Include menu HTML code -------- --%> <jsp:include
					page="/menu.html" />
			</td>
			<td>
				<form id="myForm" method="post" action="reviewsession.jsp">
					<p align="center">
						<font size="6">Review Session</font>
					</p>
					<table border="1">
						<tr>
							<th>Session ID</th>
							<th>Date Time</th>
							<th>Building Room</th>
							<th>Mandatory</th>
							<th>Action</th>
						</tr>
						<%---------- Add ----------%>
						<tr>
							<td><select name="sltClasses" id="sltClasses">
									<option value="">Select a class</option>
									<%
										stmt = conn.createStatement();
										ResultSet rsClasses = stmt.executeQuery("SELECT * FROM classes");
										while (rsClasses.next()) {
									%>
									<option value="<%=rsClasses.getInt("class_id")%>"><%=rsClasses.getInt("class_id")%></option>
									<%
										}
										rsClasses.close();
										stmt.close();
									%>
							</select></td>
							<td><input type="date" name="txtDate" id="txtDate"><input
								type="time" name="txtTime" id="txtTime"></td>
							<td><input type="text" name="txtRoom" id="txtRoom"></td>
							<td align="center"><input type="checkbox"
								name="chkMandatory" id="chkMandatory"></td>
							<td align="center"><input type="submit" value="Insert"
								onclick="Insert()"></td>
						</tr>
						<%---------- Edit ----------%>
						<%
							stmt = conn.createStatement();
							ResultSet rsReviewSections = stmt
									.executeQuery("SELECT * FROM review_sections");

							while (rsReviewSections.next()) {
						%>
						<tr>
							<td align="center"><%=rsReviewSections.getInt("section_id")%></td>
							<td><input type="date"
								name="txtDate<%=rsReviewSections.getInt("id")%>"
								id="txtDate<%=rsReviewSections.getInt("id")%>"
								value="<%=rsReviewSections.getString("date")%>"> <input
								type="time" name="txtTime<%=rsReviewSections.getInt("id")%>"
								id="txtTime<%=rsReviewSections.getInt("id")%>"
								value="<%=rsReviewSections.getString("time")%>"></td>
							<td><input type="text"
								name="txtRoom<%=rsReviewSections.getInt("id")%>"
								id="txtRoom<%=rsReviewSections.getInt("id")%>"
								value="<%=rsReviewSections.getString("building_room")%>"></td>
							<td align="center">
								<%
									if (rsReviewSections.getBoolean("mandatory") == true) {
								%> <input type="checkbox"
								name="chkMandatory<%=rsReviewSections.getInt("id")%>"
								id="chkMandatory<%=rsReviewSections.getInt("id")%>" checked>
								<%
									} else {
								%> <input type="checkbox"
								name="chkMandatory<%=rsReviewSections.getInt("id")%>"
								id="chkMandatory<%=rsReviewSections.getInt("id")%>"> <%
 	}
 %>
							</td>
							<td><input type="submit" value="Update"
								onclick="Update(<%=rsReviewSections.getInt("id")%>)"> <input
								type="submit" value="Delete"
								onclick="Delete(<%=rsReviewSections.getInt("id")%>)"></td>
						</tr>
						<%
							}
						%>
					</table>
					<input type="hidden" id="ACTION" name="ACTION" value="None">
					<input type="hidden" id="PARAMETER" name="PARAMETER" value="">
				</form>
			</td>
		</tr>
	</table>
</body>
</html>
<%
	// Close the Connection
	conn.close();
%>